package com.tbit.uqbike.client.service.impl;

import com.tbit.uqbike.client.constant.NewEventConstant;
import com.tbit.uqbike.client.dao.core.InviteNewEventDao;
import com.tbit.uqbike.client.dao.log.ActiveStatDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.ActiveStat;
import com.tbit.uqbike.client.pojo.InviteNewEvent;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.ActiveStatService;
import com.tbit.uqbike.client.service.UserActiveLogService;
import com.tbit.uqbike.client.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description: 用户参与活动统计
 * @Date: 创建于 17:31 2022/5/11
 */
@Service
public class ActiveStatServiceImpl implements ActiveStatService {
    @Autowired
    private ActiveStatDao activeStatDao;
    @Autowired
    private InviteNewEventDao inviteNewEventDao;
    @Autowired
    private UserActiveLogService userActiveLogService;

    @Override
    public ActiveStat getByPhoneAndEventId(String phone, Integer eventId, String date) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("eventId", eventId);
        map.put("date", date);
        return activeStatDao.getByPhoneAndEventId(map);
    }

    @Override
    public void add(User user, Integer eventId, Integer type) {
        InviteNewEvent inviteNewEvent = inviteNewEventDao.getByEventId(eventId);
        if (inviteNewEvent == null) {
            throw new BaseException("-190001", "活动已结束");
        }
        ActiveStat activeStat = new ActiveStat();
        activeStat.setEventType(inviteNewEvent.getType());
        activeStat.setEventId(eventId);
        activeStat.setAdAccountId(inviteNewEvent.getAccountId());
        activeStat.setPhone(user.getPhone());
        activeStat.setAccountId(user.getAccountId());
        if (type.equals(NewEventConstant.EVENT_TYPE_SHARE)) {
            activeStat.setShare(1);
        } else if (type.equals(NewEventConstant.EVENT_TYPE_WATCH)) {
            activeStat.setWatch(1);
        } else if (type.equals(NewEventConstant.EVENT_TYPE_RECEIVE)) {
            activeStat.setReceive(1);
        } else if (type.equals(NewEventConstant.EVENT_TYPE_FINISH)) {
            activeStat.setFinish(1);
        }
        String date = DateTimeUtil.getNowTimeYMD();
        activeStat.setDate(date);
        ActiveStat activeStatDB = getByPhoneAndEventId(user.getPhone(), eventId, date);
        if (activeStatDB == null) {
            activeStatDao.add(activeStat);
        } else {
            activeStatDao.edit(activeStat);
        }
        userActiveLogService.add(user.getPhone(), eventId, type, user.getUserId());
    }

    @Override
    public void edit(ActiveStat activeStat) {
        activeStatDao.edit(activeStat);
    }

    @Override
    public ActiveStat getByUserId(String phone, Integer eventId) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("eventId", eventId);
        return activeStatDao.getByUserId(map);
    }
}
